package com.twu.biblioteca;

import com.twu.biblioteca.data.Book;
import com.twu.biblioteca.data.BookShelf;
import com.twu.biblioteca.data.MovieBase;
import com.twu.biblioteca.data.UserBase;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: zgq
 * Date: 13-7-5
 * Time: 上午10:44
 * To change this template use File | Settings | File Templates.
 */
public class BibliotecaDataOperator {

    public static final String RESERVE_BOOK_SUCCESS = "Thank You! Enjoy the book";
    public static final String RESERVE_BOOK_FAIL = "Sorry we don't have that book yet";
    public static final String RESERVE_NOTICE = "In put the book name you want to reserve";
    private static final String WELCOME_TO_BIBLIOTECA = "Welcome to Biblioteca";
    public static final String INPUT_CORRECT_USERNAME_OR_PASSWORD = "Please input correct username or password";
    public static final String TALK_TO_LIBRARIAN = "Please talk to a Librarian. Thank you.";
    public static final String PLEASE_LOGIN_FIRST = "Please login first";

    public static final String BIBLIOTECA_BOOK_DATABASE_NAME = "BibliotecaBook.txt";
    public static final String BIBLIOTECA_MOVIE_DATABASE_NAME = "BibliotecaMovie.txt";
    public static final String BIBLIOTECA_USER_DATABASE_NAME = "BibliotecaUser.txt";

    private BookShelf bookShelf;
    private MovieBase movieBase;
    private UserBase userBase;

    private Session session;

    public BibliotecaDataOperator() {
        bookShelf = new BookShelf(BIBLIOTECA_BOOK_DATABASE_NAME);
        movieBase = new MovieBase(BIBLIOTECA_MOVIE_DATABASE_NAME);
        userBase = new UserBase(BIBLIOTECA_USER_DATABASE_NAME);
    }

    public String printBookInformation() {
        String bookInformation="";
        List<String> bookStringList = bookShelf.bookList();
        int [] maxSizeOfEachColumn = calculateColumnMaxSize(bookStringList);
        if(bookStringList != null) {
            Iterator bookListIterator = bookStringList.iterator();
            while(bookListIterator.hasNext()) bookInformation += formateString((String) bookListIterator.next(), maxSizeOfEachColumn)+"\n";
        }
        return bookInformation;
    }

    public String printMovieInformation() {
        List<String> movieStringList = movieBase.movieList();
        int [] maxSizeOfEachColumn = calculateColumnMaxSize(movieStringList);
        String movieInformation=formateString("Movie\tYear\tDirector\tRating", maxSizeOfEachColumn);
        if(movieStringList != null) {
            Iterator movieListIterator = movieStringList.iterator();
            while(movieListIterator.hasNext()) movieInformation += "\n"+formateString((String)movieListIterator.next(), maxSizeOfEachColumn);
        }
        return movieInformation;
    }

    public void putInBook(Book book) {
        bookShelf.putBookIntoStore(book);
    }

    public String reserveBook() {
        if(session==null) return PLEASE_LOGIN_FIRST;
        System.out.println(RESERVE_NOTICE);
        Scanner scanner=new Scanner(System.in);
        String bookName = scanner.nextLine();
        if(bookShelf.canBeReserved(bookName)) return RESERVE_BOOK_SUCCESS;
        return RESERVE_BOOK_FAIL;
    }

    public String checkMemberShip( ) {
        if(session==null) return TALK_TO_LIBRARIAN;
        return userBase.printUserInfo(session.get("userName"));
    }

    public boolean validateUser(String userName, String password) {
        return userBase.validateUser(userName, password);
    }

    public String login() {
        String userName = inputUserName();
        String password = inputPassword();
        if(validateUser(userName, password)) {
            session = new Session();
            session.put("userName", userName);
            session.put("password", password);
            return userName+","+WELCOME_TO_BIBLIOTECA;
        }
        return INPUT_CORRECT_USERNAME_OR_PASSWORD;
    }

    private String inputUserName() {
        System.out.println("Please Input Username:");
        Scanner scanner = new Scanner(System.in);
        return  scanner.nextLine();
    }

    private String inputPassword() {
        System.out.println("Input Password");
        Scanner scanner = new Scanner(System.in);
        return  scanner.nextLine();
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    private String formateString(String line, int columnMaxSize[]) {
        String [] lineItems = line.split("\t");
        String formatedString = "";
        int indexForColumnMaxSize = 0;
        for(String lineItem : lineItems) {
            while((columnMaxSize[indexForColumnMaxSize]-lineItem.getBytes().length)>0) lineItem+=" ";
            formatedString += lineItem+"\t";
            indexForColumnMaxSize++;
        }
        return formatedString;
    }

    private int[] calculateColumnMaxSize(List<String> stringList) {
        int []columnSize = new int[calculateColumnNum(stringList)];
        int indexForColumnSize;
        String [] lineItems;
        for(String line : stringList) {
            lineItems = line.split("\t");
            indexForColumnSize = 0;
            for(String lineItem : lineItems) {
                if(columnSize[indexForColumnSize]<lineItem.length()) columnSize[indexForColumnSize] = lineItem.length();
                indexForColumnSize++;
            }
        }
        return columnSize;
    }

    private int calculateColumnNum(List<String> stringList) {
         if(stringList!=null&&stringList.size()>0)
             return stringList.get(0).split("\t").length;
        return 0;
    }
}
